package com.wushijia.test.reference;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

public class ReferenceDemo1 {

  public static void main(String[] agrs) {
    String str = new String("hello world");//会放在堆内存，会被gc回收
    //String str = "hello world";//会放在常量池，不会被回收
    ReferenceQueue<String> referenceQueue = new ReferenceQueue<>();
    WeakReference weakReference = new WeakReference(str, referenceQueue);//放到引用队列
    str = null;//去除强引用
    System.gc();
    System.gc();
    System.out.println(weakReference.get());
    System.out.println(referenceQueue.poll().get());//弱引用被回收，放到了引用队列里
  }
}
